<?php

namespace App\Console\Stats;

use Illuminate\Console\Command as BaseCommand;
use App\Exceptions\Response\ServiceException;
use Helper\Timed;

class Command extends BaseCommand
{

    /**
     * 创建一个新的命令实例。
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取前1小时整点时间戳
     *
     * @return number
     */
    protected function getHourPreTimed()
    {
        return strtotime(date('Y-m-d H:00:00')) - 3600;
    }

    /**
     * 验证时间戳【小时】
     *
     * @param number $timed
     * @return \App\Console\Stats\Command
     * @throws \Exception
     */
    protected function verifyHourTimed($timed)
    {
        // 验证时间戳
        if (($timed % 3600) != 0) {
            throw new ServiceException(null, '时间戳必须是小时整点');
        } else if ($timed > $this->getHourPreTimed()) {
            throw new ServiceException(null, '时间戳必须小于当前小时');
        }
        return $this;
    }

    /**
     * 获取统计时间【整点时间戳】【小时】
     *
     * @return number
     */
    public function getHourTimed()
    {
        if ($this->hasArgument('timed') && $this->argument('timed')) {
            // 参数时间戳
            $timed = $this->argument('timed');
            // 验证时间戳
            $this->verifyHourTimed($timed);
        } else {
            // 整点时间戳【前1小时】
            $timed = $this->getHourPreTimed();
        }
        return $timed;
    }

    /**
     * 获取昨天0点时间戳
     *
     * @return number
     */
    protected function getYesterdayTimed()
    {
        return Timed::getYesterdayTimed();
    }

    /**
     * 验证时间戳【天】
     *
     * @param number $timed
     * @return \App\Console\Stats\Command
     * @throws \Exception
     */
    protected function verifyTimed($timed)
    {
        // 验证时间戳
        if (($timed % 86400) != 57600) {
            throw new ServiceException(null, '时间戳必须是每日0点');
        } else if ($timed > $this->getYesterdayTimed()) {
            throw new ServiceException(null, '时间戳必须小于今天');
        }
        return $this;
    }

    /**
     * 获取统计时间【0点时间戳】【天】
     *
     * @return number
     */
    public function getTimed()
    {
        if ($this->hasArgument('timed') && $this->argument('timed')) {
            // 参数时间戳
            $timed = $this->argument('timed');
            // 验证时间戳
            $this->verifyTimed($timed);
        } else {
            // 0点时间戳【昨天】
            $timed = $this->getYesterdayTimed();
        }
        return $timed;
    }

    /**
     * 获取上周一日0点时间戳
     *
     * @return number
     */
    protected function getWeekPreTimed()
    {
        return Timed::getWeekPreTimed();
    }

    /**
     * 验证时间戳【周一】
     *
     * @param number $timed
     * @return \App\Console\Stats\Command
     * @throws \Exception
     */
    protected function verifyWeekTimed($timed)
    {
        // 验证时间戳
        if (($timed % 86400) != 57600 || date('N', $timed) != 1) {
            throw new ServiceException(null, '时间戳必须是每周一0点');
        } else if ($timed > $this->getWeekPreTimed()) {
            throw new ServiceException(null, '时间戳必须小于当周');
        }
        return $this;
    }

    /**
     * 获取统计时间【0点时间戳】【周一】
     *
     * @return number
     */
    public function getWeekTimed()
    {
        if ($this->hasArgument('timed') && $this->argument('timed')) {
            // 参数时间戳
            $timed = $this->argument('timed');
            // 验证时间戳
            $this->verifyWeekTimed($timed);
        } else {
            // 0点时间戳【上周一】
            $timed = $this->getWeekPreTimed();
        }
        return $timed;
    }

    /**
     * 获取上月1日0点时间戳
     *
     * @return number
     */
    protected function getMonthPreTimed()
    {
        return Timed::getMonthPreTimed();
    }

    /**
     * 验证时间戳【月1日】
     *
     * @param number $timed
     * @return \App\Console\Stats\Command
     * @throws \Exception
     */
    protected function verifyMonthTimed($timed)
    {
        // 验证时间戳
        if (($timed % 86400) != 57600 || date('d', $timed) != 1) {
            throw new ServiceException(null, '时间戳必须是每月1日0点');
        } else if ($timed > $this->getMonthPreTimed()) {
            throw new ServiceException(null, '时间戳必须小于当月');
        }
        return $this;
    }

    /**
     * 获取统计时间【0点时间戳】【月1日】
     *
     * @return number
     */
    public function getMonthTimed()
    {
        if ($this->hasArgument('timed') && $this->argument('timed')) {
            // 参数时间戳
            $timed = $this->argument('timed');
            // 验证时间戳
            $this->verifyMonthTimed($timed);
        } else {
            // 0点时间戳【上月1日】
            $timed = $this->getMonthPreTimed();
        }
        return $timed;
    }
}
